package cn.just.controller;

import cn.just.entity.Article;
import cn.just.entity.User;
import cn.just.service.ArticleService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @author wujiaming
 */
@WebServlet(name = "SendActionServlet",value = "/SendActionServlet")
public class SendActionServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        //请求乱码
        request.setCharacterEncoding("UTF-8");
        String title = request.getParameter("title");
        String abs = request.getParameter("abs");
        String content = request.getParameter("content");
        String imgName = request.getParameter("imgName");
        String putTop = request.getParameter("putTop");
        int isTop = putTop==null?0:1;
        System.out.println(title +"," + abs + content + imgName + putTop);

        //将数据封装到article对象中
        Article article = new Article();
        article.setTitle(title);
        article.setAbs(abs);
        article.setContent(content);
        article.setImgName(imgName);
        article.setPutTop(isTop);
        //获得当前登录用户的哦ID 也是作者id
        HttpSession session = request.getSession();
        User user = (User)session.getAttribute("user");
        //将当前登录用户的oId 关联到article对象中
        article.setAuthorId(user.getoId());

        //调用save
        ArticleService articleService = new ArticleService();
        articleService.save(article);
        //重定向到列表页面
        response.sendRedirect(request.getContextPath() + "/ListServlet");
    }


}
